﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RecipeBookWeb
{
    public partial class CreateAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Register_Click(object sender, EventArgs e)
        {
            string username = UsernameTextBox.Text.Trim();
            //This will return -1 if the user doesn't exist
            if (RecipeBookBusiness.ModelFacade.GetUserID(username) == -1)
            {
                ErrorLabel.Visible = false;
                string firstName = FirstNameTextBox.Text.Trim();
                string lastName = LastNameTextBox.Text.Trim();
                string email = EmailTextBox.Text.Trim();
                string password = PasswordTextBox.Text;
                RecipeBookBusiness.ModelFacade.AddUser(firstName, lastName, email, password, username);
                Session["CurrentUser"] = username;
                Response.Redirect("Default.aspx");
            }
            else
            {
                ErrorLabel.Visible = true;
                ErrorLabel.Text = "Username already exists. Please select a different username.<br>";
            }
        }
    }
}
